<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>JavaScript对象 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,">
  

  <meta name="description" content="创建对象">
<meta name="keywords" content="JavaScript">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript对象">
<meta property="og:url" content="http://blog.poetries.top/2016/07/27/JavaScript对象/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="创建对象">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.898Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript对象">
<meta name="twitter:description" content="创建对象">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#创建对象"><span class="toc-text">创建对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#对象的遍历"><span class="toc-text">对象的遍历</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#对象的单元删除"><span class="toc-text">对象的单元删除</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#对象的方法"><span class="toc-text">对象的方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#JavaScript内置对象"><span class="toc-text">JavaScript内置对象:</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#日期时间对象"><span class="toc-text">日期时间对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Math对象"><span class="toc-text">Math对象</span></a></li></ol></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-JavaScript对象" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">JavaScript对象</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2016.07.27</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 1.7k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 6分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h3 id="创建对象"><a href="#创建对象" class="headerlink" title="创建对象"></a>创建对象</h3><hr>
<a id="more"></a>
<ul>
<li><p>对象的概念：</p>
<ul>
<li>对象和数组的本质一样 都是组织一堆数据</li>
<li>只不过 对象的下表不为数字而且无序</li>
<li>每个编号–&gt;内容</li>
<li>属性–&gt;值</li>
</ul>
</li>
<li><p>对象和数组的本质一样 都是组织一堆数据</p>
</li>
<li><p>只不过 对象的下表不为数字而且无序</p>
</li>
<li><p><strong>创建对象：</strong>（不仅给值 还给属性）</p>
<ul>
<li>创建对象和数组的[]不一样 </li>
<li>和创建对象用{}</li>
<li>和创建数组直接放置的值不一样 创建对象时值前面还要加上属性</li>
<li>创建语法:<code>{属性1:值1,属性2:值2}</code></li>
</ul>
</li>
</ul>
<ul>
<li><p>对象单元值的引用：（两种方法）</p>
<ul>
<li><code>Obj.属性</code></li>
<li><code>Obj[&#39;属性&#39;]</code></li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> stu = &#123;<span class="attr">name</span>:<span class="string">'小明'</span>,<span class="attr">age</span>:<span class="number">22</span>,<span class="attr">number</span>:<span class="number">007</span>,<span class="attr">score</span>:<span class="number">99</span>&#125;;</span><br><span class="line"><span class="comment">// alert(stu);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//这种方法不推荐</span></span><br><span class="line"><span class="comment">//alert(stu['name']);// name需要加上单引号 不然就和变量一样了</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//推荐用法</span></span><br><span class="line">alert(stu.score);</span><br></pre></td></tr></table></figure>
<h3 id="对象的遍历"><a href="#对象的遍历" class="headerlink" title="对象的遍历"></a>对象的遍历</h3><hr>
<ul>
<li><p><strong>遍历对象：</strong></p>
<ul>
<li>因为对象的属性和数组的下标不一样  </li>
<li>数组的下表从0开始并且有规律的递增  因此可以用for循环遍历</li>
</ul>
<ul>
<li>对象的属性 没有规律的</li>
<li>for in结构来遍历<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">For(per in Obj)&#123;</span><br><span class="line">  ......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
</ul>
<p>注意：在<code>for in</code>结构中循环得到的属性取值时不能用<code>Obj.属性</code>的方式  <code>只能用中括号</code></p>
<ul>
<li>对象和数组本质一个键值对</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> stu = &#123;<span class="attr">name</span>:<span class="string">'小明'</span>,<span class="attr">age</span>:<span class="number">22</span>,<span class="attr">number</span>:<span class="number">007</span>,<span class="attr">score</span>:<span class="number">99</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//注意：在for in 结构中循环得到的属性取值时不能用Obj.属性的方式  只能用obj[属性]</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">var</span> i <span class="keyword">in</span> stu)&#123;<span class="comment">//把stu里面的所有属性依次赋给i遍历输出</span></span><br><span class="line">		<span class="built_in">document</span>.write(stu[i]+<span class="string">'&lt;br&gt;'</span>);<span class="comment">//stu['i'] 不能这样写   stu['name'] 必须加上单引号 否则系统理解为name是变量  name是属性 是一个字符</span></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h3 id="对象的单元删除"><a href="#对象的单元删除" class="headerlink" title="对象的单元删除"></a>对象的单元删除</h3><hr>
<ul>
<li>对象的单元删除:对象单元的删除delete obj.属性</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> stu = &#123;<span class="attr">name</span>:<span class="string">'小明'</span>,<span class="attr">age</span>:<span class="number">22</span>,<span class="attr">number</span>:<span class="number">007</span>,<span class="attr">score</span>:<span class="number">99</span>&#125;;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">var</span> i <span class="keyword">in</span> stu)&#123;</span><br><span class="line">		<span class="built_in">document</span>.write(stu[i]+<span class="string">'&lt;br&gt;'</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">document</span>.write(<span class="string">'&lt;hr&gt;'</span>);</span><br><span class="line">	<span class="keyword">delete</span> stu.score;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">var</span> i <span class="keyword">in</span> stu)&#123;</span><br><span class="line">		<span class="built_in">document</span>.write(stu[i]+<span class="string">'&lt;br&gt;'</span>);</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h3 id="对象的方法"><a href="#对象的方法" class="headerlink" title="对象的方法"></a>对象的方法</h3><hr>
<ul>
<li>在js中 函数本身就是变量 而在数组和对象中存储的就是‘变量’</li>
<li>对象的某个属性的值–没有可能是一个哈函数？也是可以的</li>
<li><p>对象的某个属性对象的值可以是函数，如果是函数时，这个属性叫做方法</p>
</li>
<li><p>如果对象的某个方法（属性），需要调用自身的某个属性值，可以在函数中用一个关键词this来代替自己这个对象</p>
<ul>
<li><code>this</code>–&gt;指向当前正在调用这个方法的对象（对象自身）</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//写法1</span></span><br><span class="line"><span class="keyword">var</span> stu = &#123;<span class="attr">name</span>:<span class="string">'张三'</span>,<span class="attr">age</span>:<span class="number">20</span>,<span class="attr">talk</span>:<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;alert(<span class="string">'哈哈哈'</span>);&#125;&#125;;</span><br><span class="line"><span class="comment">// alert(stu.talk);</span></span><br><span class="line">stu.talk();<span class="comment">//调用这个函数</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//写法2</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> zhang = &#123;<span class="attr">name</span>:<span class="string">'张三'</span>,<span class="attr">age</span>:<span class="number">20</span>,<span class="attr">talk</span>:<span class="literal">null</span>&#125;;</span><br><span class="line"><span class="keyword">var</span> lisi = &#123;<span class="attr">name</span>:<span class="string">'李四'</span>,<span class="attr">age</span>:<span class="number">22</span>,<span class="attr">talk</span>:<span class="literal">null</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">t</span>(<span class="params">pname</span>)</span>&#123;</span><br><span class="line">	alert(<span class="string">'你好'</span>+pname);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">zhang.talk = t;</span><br><span class="line">zhang.talk(zhang.name);</span><br><span class="line"></span><br><span class="line">lisi.talk = t;</span><br><span class="line">lisi.talk(lisi.name);</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//写法3  推荐</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> zhang = &#123;<span class="attr">name</span>:<span class="string">'张三'</span>,<span class="attr">age</span>:<span class="number">20</span>,<span class="attr">talk</span>:<span class="literal">null</span>&#125;;</span><br><span class="line"><span class="keyword">var</span> lisi = &#123;<span class="attr">name</span>:<span class="string">'李四'</span>,<span class="attr">age</span>:<span class="number">22</span>,<span class="attr">talk</span>:<span class="literal">null</span>&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">t</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	alert(<span class="string">'你好'</span>+<span class="keyword">this</span>.name);<span class="comment">//this指向当前正在调用这个方法的对象</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">zhang.talk = t;</span><br><span class="line">zhang.talk();</span><br><span class="line"></span><br><span class="line">lisi.talk = t;</span><br><span class="line">lisi.talk();</span><br></pre></td></tr></table></figure>
<h3 id="JavaScript内置对象"><a href="#JavaScript内置对象" class="headerlink" title="JavaScript内置对象:"></a>JavaScript内置对象:</h3><hr>
<ul>
<li><p>在js中 所有的变量 都可以被js引擎包装秤对象来处理</p>
</li>
<li><p>比如：<code>str=&#39;abd&#39;</code>字符串本身没有<code>length</code>属性</p>
</li>
<li><p>但是你去调用<code>str.length</code> 在调用一瞬间<code>js</code>引擎会把它包装成一下 当做对象来处理 并且给这个赋了一些方法</p>
</li>
<li><p>对于字符串 布尔值 数值类型 数组 <code>null</code>这些变量 虽然没有属性 但是在调用的前一瞬间 js会为他们包装一些属性和方法</p>
</li>
<li><p>还有一些内置对象，是通过<code>new</code>得来的 比如：日期时间对象 先<code>new</code> 在调用方法和属性</p>
<ul>
<li><code>Math</code>对象  和字符串一样 不用<code>new</code>也能直接其方法</li>
</ul>
</li>
</ul>
<ul>
<li><p><strong>字符串对象的属性和方法：</strong></p>
<ul>
<li><p><code>length</code>属性： 长度</p>
</li>
<li><p><code>concat</code>方法：连接两个或者更多个字符串</p>
</li>
<li><p><code>indexOf（String）</code>返回出现字符串的位置  找不到就返回-1</p>
</li>
<li><p><code>substr(num1,[num2])</code>//截取字符串  num2截取的宽度</p>
</li>
<li><p><code>toUpperCase()</code>转换成大写</p>
</li>
<li><p><code>toLowerCase()</code>转换成小写</p>
</li>
<li><p><code>replace(oldstr1,newstr2)</code>//字符串替换</p>
</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> str = <span class="string">'helloworld'</span>;<span class="comment">//这是一个字符串变量</span></span><br><span class="line"> <span class="comment">// alert(str.length);</span></span><br><span class="line"> </span><br><span class="line"> alert(str.concat(<span class="string">'中国'</span>));<span class="comment">//concat方法：连接两个或者更多个字符串</span></span><br><span class="line"></span><br><span class="line"> alert(str.indexOf(<span class="string">'o'</span>));<span class="comment">//4 </span></span><br><span class="line"></span><br><span class="line"> alert(str.substr(str.indexOf(<span class="string">'o'</span>)));<span class="comment">//substr截取字符串 一般从开始位置截取 </span></span><br><span class="line"> 									<span class="comment">//如有特殊：需要指定截取位置 indexOf（String）返回出现字符串的位置  从当前位置开始截取</span></span><br><span class="line"> 									</span><br><span class="line"> alert(str.toUpperCase());</span><br><span class="line"> alert(str.replace(<span class="string">'hello'</span>,<span class="string">'连英'</span>));<span class="comment">// replace(oldstr1,newstr2) 字符串替换</span></span><br></pre></td></tr></table></figure>
<h4 id="日期时间对象"><a href="#日期时间对象" class="headerlink" title="日期时间对象"></a>日期时间对象</h4><hr>
<ul>
<li>日期时间对象 必须通过new 来得到</li>
<li>早数组可以<code>new Array()</code></li>
<li><p>造日期时间对象 <code>new Date();</code></p>
</li>
<li><p><code>getYear()</code>  返回年份（2位或4位）</p>
</li>
<li><code>getFullYear()</code> 返回年份（4位）</li>
<li><code>getMonth()</code>返回月份（0-11）</li>
<li><code>getDate()</code> 返回日期 1-31</li>
<li><code>getDay()</code>  返回星期数  0-6</li>
<li><code>getHours()</code>返回小时数0-23</li>
<li><code>getMinutes()</code>返回分钟数 0-59</li>
<li><code>getSeconds()</code>返回秒数  0-59</li>
<li><code>getMilliseconds()</code> 返回毫秒数0-999</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> time = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">alert(time.getYear());<span class="comment">//116</span></span><br><span class="line">alert(time.getFullYear());<span class="comment">//2016</span></span><br><span class="line">alert(time.getMonth());<span class="comment">//5</span></span><br><span class="line">alert(time.getDate());<span class="comment">//5</span></span><br><span class="line">alert(time.getDay());<span class="comment">// 星期天 0</span></span><br><span class="line">alert(time.getHours());<span class="comment">//1</span></span><br><span class="line">alert(time.getSeconds());<span class="comment">//17</span></span><br><span class="line">alert(time.getMinutes());<span class="comment">//20</span></span><br><span class="line">alert(time.getMilliseconds());</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//页面上动态时钟</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">t</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//把年月日 拼接起来</span></span><br><span class="line">    <span class="keyword">var</span> time = <span class="keyword">new</span> <span class="built_in">Date</span>();</span><br><span class="line">    <span class="keyword">var</span> year = time.getFullYear();</span><br><span class="line">    <span class="keyword">var</span> month = time.getMonth();</span><br><span class="line">    <span class="keyword">var</span> day = time.getDate();</span><br><span class="line">    <span class="keyword">var</span> hour = time.getHours();</span><br><span class="line">    <span class="keyword">var</span> minute = time.getMinutes();</span><br><span class="line">    <span class="keyword">var</span> second = time.getSeconds();</span><br><span class="line"></span><br><span class="line">    <span class="built_in">document</span>.getElementById(<span class="string">'clock'</span>).value = (year+<span class="string">" "</span>+month+<span class="string">" "</span>+day+<span class="string">" "</span>+hour+<span class="string">":"</span>+minute+<span class="string">":"</span>+second);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// document.getElementById('clock').value = t();  //error</span></span><br><span class="line">    <span class="keyword">var</span> clock = setInterval(<span class="string">'t()'</span>,<span class="number">1000</span>);</span><br></pre></td></tr></table></figure>
<h4 id="Math对象"><a href="#Math对象" class="headerlink" title="Math对象"></a>Math对象</h4><hr>
<ul>
<li><p><code>Math</code>对象  和字符串一样 不用<code>new</code>也能直接其方法</p>
<ul>
<li><code>ceil(数值)</code>大于或等于该数的最小整数</li>
<li><code>floor(数值)</code> 小于或等于该数的最大整数</li>
<li><code>min(数值1,数值2)</code> 返回最小值</li>
<li><code>max()</code>返回最大值</li>
<li><code>pow(num1,num2)</code>//返回num1的那么次方</li>
<li><code>random()</code>返回随机数  0–1</li>
<li><code>round(数值)</code>四舍五入 </li>
<li><code>sqrt(数值)</code>开平方根</li>
</ul>
</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">20</span>; i++) &#123;</span><br><span class="line"><span class="built_in">document</span>.write(<span class="number">5</span>+<span class="built_in">Math</span>.random()*<span class="number">5</span>+<span class="string">'&lt;br&gt;'</span>);<span class="comment">//5-10之间的随机</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">alert(<span class="built_in">Math</span>.round(<span class="number">3.6658</span>));<span class="comment">//4</span></span><br><span class="line">alert(<span class="built_in">Math</span>.sqrt(<span class="number">2</span>));<span class="comment">//1.4</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">//输入任意两个数字 返回两个数之间的随机数的整数 </span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">rd</span>(<span class="params">big,small</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">Math</span>.ceil(<span class="built_in">Math</span>.random()*(big-small))+small;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">30</span>; i++) &#123;</span><br><span class="line">    <span class="built_in">document</span>.write(rd(big,small)+<span class="string">'&lt;br&gt;'</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//javascript中prompt方法可以让用户输入一个文本，从而作为返回值 prompt返回的是字符串类型</span></span><br><span class="line"><span class="comment">//需要用到parseInt 转成数字型  比如：输入12  实际返回的是'12'</span></span><br><span class="line"><span class="comment">//alert(typeof(window.prompt()));//string</span></span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2016/07/26/JavaScript词法分析和作用域闭包/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2016/07/27/JavaScript之面向对象中的静态方法-静态属性/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
